package com.otoe.dilidilivid.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;

import com.otoe.dilidilivid.R;

import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.otoe.dilidilivid.view.fragment.FragmentHome;
import com.otoe.dilidilivid.view.fragment.FragmentMy;
import com.otoe.dilidilivid.view.fragment.FragmentUpload;

public class MainActivity extends AppCompatActivity {

    private FrameLayout mainFrame;
    private BottomNavigationView bottomNavigation;
    private FragmentHome homeFragment;
    private FragmentUpload uploadFragment;
    private FragmentMy myFragment;
    private Fragment[] fragments;
    private int lastfragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        homeFragment = new FragmentHome();
        uploadFragment = new FragmentUpload();
        myFragment = new FragmentMy();
        fragments = new Fragment[]{homeFragment, uploadFragment, myFragment};
        mainFrame = (FrameLayout) findViewById(R.id.fl_show);
        //设置fragment到布局
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_show, homeFragment).show(homeFragment).commit();

        bottomNavigation = (BottomNavigationView) findViewById(R.id.bnv_show);

        //这里是bottomnavigationview的点击事件
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home_btn:
                    //这里需要对3个fragment进行切换

                    if (lastfragment != 0) {
                        switchFragment(lastfragment, 0);
                        lastfragment = 0;
                    }
                    return true;
                case R.id.upload_btn:
                    if (lastfragment != 1) {
                        switchFragment(lastfragment, 1);
                        lastfragment = 1;
                    }
                    startActivity(new Intent(getApplicationContext(), VideoUploadActivity.class));
                    return true;
                case R.id.my_btn:
                    if (lastfragment != 2) {
                        switchFragment(lastfragment, 2);
                        lastfragment = 2;
                    }
                    return true;
                default:
                    break;
            }
            return false;
        }
    };

    /*切换fragment*/
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隐藏上个Fragment
        transaction.hide(fragments[lastfragment]);
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.fl_show, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }
}